package com.yangfan.screenshot.ui

import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern
import com.lzf.easyfloat.enums.SidePattern
import com.yangfan.screenshot.R

/**
 * @author : yangfan
 * @date : 2022/12/15-13:17
 * desc   : 悬浮按钮
 */
class FloatButtonWindow(context: Context?, listener: BtnListener?) {
    private val mContext: Context? = null
    var floatBtn: ViewGroup

    /**
     * 短截图按钮
     */
    var shotStartTv: TextView

    /**
     * 长截图按钮
     */
    var shotLongTv: TextView

    companion object {
        /**
         * 取消次数，第3次取消才响应
         */
        var cancelNumber: Int = 0
    }

    interface BtnListener {
        /**
         * 短截图
         */
        fun onShotBtnClick()

        /**
         * 长截图
         */
        fun onLongBtnClick()

        /**
         * 取消
         */
        fun onCancelClick()

        /**
         * 完成
         */
        fun onCompleteClick()

        /**
         * 关闭
         */
        fun onCloseClick()
    }

    init {
        floatBtn =
            LayoutInflater.from(context).inflate(R.layout.layout_float_view, null) as ViewGroup
        shotStartTv = floatBtn.findViewById(R.id.shot_start)
        shotLongTv = floatBtn.findViewById(R.id.long_start)
        shotStartTv.setOnClickListener {
            floatBtn.visibility = View.GONE
            floatBtn.postDelayed({ listener?.onShotBtnClick() }, 500)
        }
        shotLongTv.setOnClickListener {
            visibilityTopTwoButton(false)
            floatBtn.visibility = View.GONE
            floatBtn.postDelayed({ listener?.onLongBtnClick() }, 500)
        }
        floatBtn.findViewById<View>(R.id.complete).setOnClickListener {
            listener?.onCompleteClick()
            visibilityTopTwoButton(true)

            cancelNumber = 0
        }
        floatBtn.findViewById<View>(R.id.cancel).setOnClickListener {
            listener?.onCancelClick()
            visibilityTopTwoButton(true)

            cancelNumber = 0
        }
        floatBtn.findViewById<View>(R.id.close).setOnClickListener {
            listener?.onCloseClick()
            visibilityTopTwoButton(true)

            cancelNumber = 0
        }

        // 启动悬浮窗
        EasyFloat.with(context!!)
            .setShowPattern(ShowPattern.ALL_TIME)
            .setSidePattern(SidePattern.RESULT_HORIZONTAL)
            .setLayout(floatBtn)
            .setGravity(Gravity.LEFT or Gravity.CENTER_VERTICAL)
            .show()
    }

    /**
     * 如何展示上方两个按钮
     */
    fun visibilityTopTwoButton(isVisible: Boolean) {
        shotStartTv.visibility = if (isVisible) View.VISIBLE else View.GONE
        shotLongTv.visibility = if (isVisible) View.VISIBLE else View.GONE
    }

    fun hide() {
        floatBtn.post { floatBtn.visibility = View.GONE }
    }

    fun show() {
        floatBtn.post { floatBtn.visibility = View.VISIBLE }
    }
}